package com.thinkgsp.webapp.controller;

import java.sql.Date;

import org.jfree.util.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.thinkgsp.jxc.service.PeriodEndForwardService;
import com.thinkgsp.sys.model.SysAccountPeriod;
import com.thinkgsp.webapp.util.ExtReturn;

/**
 * 期末结转控制器
 * @author evan
 *
 */
@Controller
@RequestMapping(value="/periodendforward")
public class PeriodEndForwardController {

	@Autowired
	private PeriodEndForwardService periodEndForwardService;
	
	@RequestMapping(value="/curPeriod.do")
	@ResponseBody
	public Object getCurrentPeriod() {
		return periodEndForwardService.getCurrentPeriod();
	}
	@RequestMapping(value="/nextPeriod.do")
	@ResponseBody
	public Object getNextPeriod() {
		return periodEndForwardService.getNextPeriod();
	}
	@RequestMapping(value="/periodForward.do")
	@ResponseBody
	public Object periodEndForward(@RequestParam("curYear")int curYear,
								   @RequestParam("curPeriod")int curPeriod,
								   @RequestParam("nextYear")int nextYear,
								   @RequestParam("nextPeriod")int nextPeriod) {
		SysAccountPeriod cur = periodEndForwardService.getPeriod(curYear, curPeriod);
		//SysAccountPeriod next = periodEndForwardService.getPeriod(nextYear, nextPeriod);
		SysAccountPeriod next = new SysAccountPeriod();
		next.setFyear(nextYear);
		next.setPeriod(nextPeriod);
		next.setBeginDate(new Date(System.currentTimeMillis()));
		next.setCurrent(false);
		if(next.periodNumber().compareTo(cur.periodNumber()) > 0) {
			try {
				periodEndForwardService.periodEndForward(cur, next);
				return new ExtReturn(true,"结转完成!");
			} catch(Exception e) {
				Log.error(e.getMessage(),e);
				return new ExtReturn("结转失败!原因:" + e.getMessage());
			}
		} else {
			return new ExtReturn("下一期间必须大于当前期间!");
		}
	}
}
